package cn.vivi.design.proxy.jdkproxy;

import cn.vivi.design.proxy.staticproxy.SellTicket;
import cn.vivi.design.proxy.staticproxy.TrainStation;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyFactory {

    private TrainStation station = new TrainStation();

    public SellTicket getProxyObject() {
        SellTicket proxyObj = (SellTicket) Proxy.newProxyInstance(station.getClass().getClassLoader(), station.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("收取一些代理费用");
                method.invoke(station);
                return null;
            }
        });

        return proxyObj;
    }

}
